<template>
    <a-modal :title="modal.title" :width="600" :confirmLoading="submitLoading" :visible="modal.visible" @ok="handleOk" @cancel="modal.visible=false">
         <form-create v-model="form.fApi" :rule="form.rule" :option="form.option" @btn-preview-click="previewClick"/>
    </a-modal>
</template>
<script>
import store from '@/store'
import { 
    allOnlineTaskAdd,
    allOnlineTaskEdit
} from '@/api/manage/task'
import { 
    allsettleEnrollSign,
    allsettleBatchSign
} from '@/api/manage/sign'
import {
    OPTION_BLOCK,
    FORM_TASK_SIGN
} from './form'
import { previewFile } from '@/utils/util'
import { mapGetters } from 'vuex'
export default {
    name: 'taskSign',
    data () {
        return {
            modal:{
                title:'新增签约',
                visible:false
            },
            form:{
                fApi:{},
                rule:FORM_TASK_SIGN,
                option:OPTION_BLOCK
            },
            selectItem:[],
            pactTempId:'',
            taskId:'',
            submitLoading:false
        }
    },
    computed: {
        ...mapGetters(['taskField','userInfo','enterAccount'])
    },
    methods:{
        add(itemarr,obj){
            this.selectItem = itemarr;
            this.pactTempId = obj.pactTempId;
            this.taskId = obj.taskId;
            this.modal.visible = true;
            this.$nextTick(()=>{
                 this.form.fApi.setValue({'parkName':obj.parkName,'pactUrl':obj.pactUrl,'type':''})
            });
            
        },
        previewClick(){
            const fileUrl = this.form.fApi.getValue('pactUrl');
            previewFile(fileUrl);
        },
        handleOk(){
            this.form.fApi.submit(async (formData, $f) => {
                try {
                    let params = {};
                    params.taskId = this.taskId;
                    params.type = formData.type;
                    params.pactTempId = this.pactTempId;
                    this.selectItem.forEach(v => {
                        v.username = v.userName
                    });
                    params.signRecords = this.selectItem;
                    this.submitLoading = true;
                    const {code} = await allsettleBatchSign(params);
                    this.submitLoading = false;
                    if(code === '1'){
                        this.$message.success('操作成功');
                        this.modal.visible = false;
                        this.$emit("on-ok");
                    }
                } catch (error) {
                    this.$message.info(JSON.stringify(error));
                }
            })
        }
    }
}
</script>